﻿using System.Linq.Expressions;
using static System.Linq.Expressions.Expression;
using ExpressionTreeToString;

namespace _21表达式树;

class Program
{
    class Book
    {
        public string Name { get; set; }
        public int Price { get; set; }
        public string Auther { get; set; }
    }

    static void Main(string[] args)
    {
        Expression<Func<Book, bool>> e1 = book => book.Price > 5;
        Func<Book, bool> f1 = book => book.Price > 5;
        Console.WriteLine(e1.ToString("Factory methods", "C#"));

        /*
        //用代码动态构建表达式树
        ParameterExpression b1 = Expression.Parameter(typeof(Book),"book");
        ConstantExpression right = Expression.Constant(5);
        MemberExpression left = Expression.MakeMemberAccess(b1, typeof(Book).GetProperty("Price"));
        BinaryExpression expType = Expression.GreaterThan(left,right);
        Expression<Func<Book, bool>> expRoot = Expression.Lambda<Func<Book, bool>>(expType, b1);
        Book[] books = new[]
        {
            new Book() { Name = "a1",Price = 5},
            new Book() { Name = "a2" ,Price = 6},
            new Book() { Name = "a3" ,Price = 7},
            new Book() { Name = "a4" ,Price = 8},
            new Book() { Name = "a5" ,Price = 9},
        };
        var result = books.Where(expRoot.Compile());
        foreach (var book in result)
        {
            Console.WriteLine(book.Name);
        }*/


        var book = Parameter(typeof(Book), "book");

        var expression = Lambda(GreaterThan(MakeMemberAccess(book, typeof(Book).GetProperty("Price")), Constant(5 )), book);
    }
}